using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ExitGames.Client.Photon;
using I2.Loc;
using UnityEngine;

public class PixelmonController : MonoBehaviour
{
	[Serializable]
	private struct Pixelmon
	{
		public string namePixelmon;

		public GameObject go;
	}

	public static PixelmonController instance;

	[SerializeField]
	public static int maxPixelmonsForWin = 5;

	[SerializeField]
	public static float timeSpawnPixelmon = 30f;

	[SerializeField]
	public static float timeRound = 300f;

	[SerializeField]
	private List<Pixelmon> _listPixelmons = new List<Pixelmon>();

	[SerializeField]
	private Material _redMaterial;

	[SerializeField]
	private Material _blueMaterial;

	[SerializeField]
	private Color _ourTeamColor;

	[SerializeField]
	private Color _enemyTeamColor;

	[SerializeField]
	private Material _blackRedMaterial;

	[SerializeField]
	private Material _blackBlueMaterial;

	[SerializeField]
	private Color _ourBlackTeamColor;

	[SerializeField]
	private Color _enemyBlackTeamColor;

	public List<SpawnPointChief.SpawnPoint> teamSpawnRed = new List<SpawnPointChief.SpawnPoint>();

	public List<SpawnPointChief.SpawnPoint> teamSpawnBlue = new List<SpawnPointChief.SpawnPoint>();

	public List<SpawnPointChief.SpawnPoint> pointSpawnPixelmon = new List<SpawnPointChief.SpawnPoint>();

	[HideInInspector]
	public List<PixelmonBehaviour> actualPixelmons = new List<PixelmonBehaviour>();

	private int _currentSpawnPixelmon;

	public string _roomPropertiesPixelmon = string.Empty;

	public string _roomPropertiesPixelmonPoints = string.Empty;

	private PhotonView _photonView;

	[SerializeField]
	private AudioSource _audioCaughtPixelmon;

	private Dictionary<string, int> _dictionaryPixelmons = new Dictionary<string, int>
	{
		{ "Busalbaur", -1 },
		{ "Butaz", -1 },
		{ "Buttlyfer", -1 },
		{ "Chadenmarr", -1 },
		{ "Eve", -1 },
		{ "Pachiku", -1 },
		{ "Pedige", -1 },
		{ "Squitrel", -1 },
		{ "Swokpole", -1 },
		{ "Tarata", -1 }
	};

	private string keyPoolParticle = "pixelmon_pick_up";

	private bool showEndWindow;

	public List<string> singleMessagePixelmon = new List<string>();

	[SerializeField]
	private int maxCountPixelmonOnScene = 2;

	[HideInInspector]
	public bool hasPixelmon;

	private List<string> _redTeamCapturedPixelmon = new List<string>();

	private List<string> _blueTeamCapturedPixelmon = new List<string>();

	[SerializeField]
	private DocStantion _redDocStantion;

	[SerializeField]
	private DocStantion _blueDocStantion;

	public TDMResult winnerTeam = TDMResult.None;

	private PointerPanel _pointer;

	public static event Action MeCatchPixelmon;

	public static event Action MeDropPixelmon;

	private void Awake()
	{
		if (GameController.instance.curTypeGame != TypeGame.Pixelmon)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		instance = this;
		_photonView = GetComponent<PhotonView>();
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void OnEnable()
	{
		PVPManager.onPVPStarted += StartGame;
		PixelmonBehaviour.OnPixelmonCollision += TryCatckPixelmon;
		DocStantion.OnStantionCollision += DropPixelmonInDocStantion;
		StartCoroutine(CheckTimeSpawn());
	}

	private void OnDisable()
	{
		PVPManager.onPVPStarted -= StartGame;
		PixelmonBehaviour.OnPixelmonCollision -= TryCatckPixelmon;
		DocStantion.OnStantionCollision -= DropPixelmonInDocStantion;
		StopCoroutine(CheckTimeSpawn());
	}

	public void ShowCapturedPixelmonParticle(Vector3 position)
	{
		GameObject gameObject = PoolManager.instance.TakeFromPool(keyPoolParticle, base.transform);
		gameObject.transform.position = position;
		gameObject.SetActive(true);
		_audioCaughtPixelmon.transform.position = position;
		_audioCaughtPixelmon.volume = VolumeController.VolumeSound;
		_audioCaughtPixelmon.Play();
		StartCoroutine(PushToPool(gameObject));
	}

	private IEnumerator PushToPool(GameObject particle)
	{
		yield return new WaitForSeconds(3f);
		PoolManager.instance.ReturnToPool(keyPoolParticle, particle);
		_audioCaughtPixelmon.transform.position = Vector3.zero;
	}

	private IEnumerator CheckTimeSpawn()
	{
		while (true)
		{
			if (PhotonNetwork.isMasterClient && PVPManager.instance != null && PVPManager.instance.gameIsStarted)
			{
				double time = (double)timeRound - ((double)(int)PVPManager.timeRound - PVPManager.instance.GetTime());
				if ((double)(timeSpawnPixelmon * (float)_currentSpawnPixelmon) <= time)
				{
					SpawnNextPixelmon();
				}
			}
			yield return new WaitForSeconds(1f);
		}
	}

	public void StartGame()
	{
		winnerTeam = TDMResult.None;
		if (PhotonNetwork.isMasterClient && GameController.instance.curTypeGame == TypeGame.Pixelmon)
		{
			GenerateListPixelmonsAndPoints();
		}
	}

	private void Check()
	{
		if (!PhotonNetwork.isMasterClient || showEndWindow)
		{
			return;
		}
		if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.red.ToString()) && (int)PhotonNetwork.room.customProperties[PunTeams.Team.red.ToString()] >= maxPixelmonsForWin)
		{
			if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.blue.ToString()) && (int)PhotonNetwork.room.customProperties[PunTeams.Team.blue.ToString()] >= maxPixelmonsForWin)
			{
				_photonView.RPC("EndPixelmonRound", PhotonTargets.All, PunTeams.Team.none);
			}
			else
			{
				_photonView.RPC("EndPixelmonRound", PhotonTargets.All, PunTeams.Team.red);
			}
			showEndWindow = true;
		}
		if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.blue.ToString()) && (int)PhotonNetwork.room.customProperties[PunTeams.Team.blue.ToString()] >= maxPixelmonsForWin)
		{
			if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.red.ToString()) && (int)PhotonNetwork.room.customProperties[PunTeams.Team.red.ToString()] >= maxPixelmonsForWin)
			{
				_photonView.RPC("EndPixelmonRound", PhotonTargets.All, PunTeams.Team.none);
			}
			else
			{
				_photonView.RPC("EndPixelmonRound", PhotonTargets.All, PunTeams.Team.blue);
			}
			showEndWindow = true;
		}
	}

	public void EndGame()
	{
		singleMessagePixelmon.Clear();
		showEndWindow = false;
		hasPixelmon = false;
		DestroyPhotonObject();
		_redDocStantion.ClearPixelmon();
		_blueDocStantion.ClearPixelmon();
		_redTeamCapturedPixelmon.Clear();
		_blueTeamCapturedPixelmon.Clear();
		if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.red.ToString()))
		{
			PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { 
			{
				PunTeams.Team.red.ToString(),
				0
			} });
		}
		if (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.blue.ToString()))
		{
			PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { 
			{
				PunTeams.Team.blue.ToString(),
				0
			} });
		}
		if (GameInfoWindow.instance != null)
		{
			GameInfoWindow.instance.tdmInfoUI.pixelmonGrid.UpdateGrid(null, null, null, false);
		}
		foreach (string item in _dictionaryPixelmons.Keys.ToList())
		{
			_dictionaryPixelmons[item] = -1;
		}
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			listPlayer.SwitchFlag(false, string.Empty);
		}
	}

	private void UpdateColorObjects()
	{
		if (TDMController.myTeam == PunTeams.Team.blue)
		{
			_blueMaterial.color = _ourTeamColor;
			_redMaterial.color = _enemyTeamColor;
			_blackBlueMaterial.color = _ourBlackTeamColor;
			_blackRedMaterial.color = _enemyBlackTeamColor;
		}
		else if (TDMController.myTeam == PunTeams.Team.red)
		{
			_blueMaterial.color = _enemyTeamColor;
			_redMaterial.color = _ourTeamColor;
			_blackBlueMaterial.color = _enemyBlackTeamColor;
			_blackRedMaterial.color = _ourBlackTeamColor;
		}
	}

	private void DestroyPhotonObject()
	{
		if (!PhotonNetwork.isMasterClient)
		{
			return;
		}
		List<Transform> list = base.transform.Cast<Transform>().ToList();
		foreach (Transform item in list)
		{
			if (item.GetComponent<PhotonView>() != null)
			{
				PhotonNetwork.Destroy(item.gameObject);
			}
		}
	}

	private void GenerateListPixelmonsAndPoints()
	{
		int num = 0;
		List<int> list = new List<int>();
		for (int i = 0; i < _listPixelmons.Count; i++)
		{
			list.Add(i);
		}
		List<int> list2 = new List<int>();
		for (int j = 0; j < pointSpawnPixelmon.Count; j++)
		{
			list2.Add(j);
		}
		_currentSpawnPixelmon = 0;
		_roomPropertiesPixelmon = string.Empty;
		_roomPropertiesPixelmonPoints = string.Empty;
		do
		{
			int index = UnityEngine.Random.Range(0, list.Count);
			_roomPropertiesPixelmon = _roomPropertiesPixelmon + list[index] + " ";
			list.RemoveAt(index);
			index = UnityEngine.Random.Range(0, list2.Count);
			_roomPropertiesPixelmonPoints = _roomPropertiesPixelmonPoints + list2[index] + " ";
			list2.RemoveAt(index);
			num++;
		}
		while (num < _listPixelmons.Count);
		ExitGames.Client.Photon.Hashtable customProperties = PhotonNetwork.room.customProperties;
		if (customProperties.ContainsKey("ListIndexPoint"))
		{
			customProperties["ListIndexPoint"] = _roomPropertiesPixelmonPoints;
		}
		else
		{
			customProperties.Add("ListIndexPoint", _roomPropertiesPixelmonPoints);
		}
		if (customProperties.ContainsKey("ListIndexPixelmon"))
		{
			customProperties["ListIndexPixelmon"] = _roomPropertiesPixelmon;
		}
		else
		{
			customProperties.Add("ListIndexPixelmon", _roomPropertiesPixelmon);
		}
		if (customProperties.ContainsKey("IndesSpawnPixelmon"))
		{
			customProperties["IndesSpawnPixelmon"] = _currentSpawnPixelmon;
		}
		else
		{
			customProperties.Add("IndesSpawnPixelmon", _currentSpawnPixelmon);
		}
		PhotonNetwork.room.SetCustomProperties(customProperties);
	}

	private void SpawnNextPixelmon()
	{
		int num = (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.red.ToString()) ? ((int)PhotonNetwork.room.customProperties[PunTeams.Team.red.ToString()]) : 0);
		int num2 = (PhotonNetwork.room.customProperties.ContainsKey(PunTeams.Team.blue.ToString()) ? ((int)PhotonNetwork.room.customProperties[PunTeams.Team.blue.ToString()]) : 0);
		if (_currentSpawnPixelmon - num - num2 >= maxCountPixelmonOnScene)
		{
			return;
		}
		string[] array = _roomPropertiesPixelmon.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		string[] array2 = _roomPropertiesPixelmonPoints.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		int result = 0;
		int result2 = 0;
		if (array.Length <= _currentSpawnPixelmon)
		{
			return;
		}
		int.TryParse(array[_currentSpawnPixelmon], out result);
		if (array2.Length <= _currentSpawnPixelmon)
		{
			return;
		}
		int.TryParse(array2[_currentSpawnPixelmon], out result2);
		if (_dictionaryPixelmons[_listPixelmons[result].namePixelmon] == -1 && !actualPixelmons.Select((PixelmonBehaviour x) => x.namePixelmon).ToList().Contains(_listPixelmons[result].namePixelmon) && !_redTeamCapturedPixelmon.Contains(_listPixelmons[result].namePixelmon) && !_blueTeamCapturedPixelmon.Contains(_listPixelmons[result].namePixelmon))
		{
			Ray ray = default(Ray);
			RaycastHit hitInfo = default(RaycastHit);
			ray.origin = pointSpawnPixelmon[result2].position + Vector3.up;
			ray.direction = Vector3.down;
			if (Physics.Raycast(ray, out hitInfo, 10000f))
			{
				GameObject gameObject = PhotonNetwork.InstantiateSceneObject("Pixelmon/" + _listPixelmons[result].namePixelmon, hitInfo.point, pointSpawnPixelmon[result2].rotation, 0, null);
			}
		}
		_currentSpawnPixelmon++;
		ExitGames.Client.Photon.Hashtable customProperties = PhotonNetwork.room.customProperties;
		if (customProperties.ContainsKey("IndesSpawnPixelmon"))
		{
			customProperties["IndesSpawnPixelmon"] = _currentSpawnPixelmon;
		}
		else
		{
			customProperties.Add("IndesSpawnPixelmon", _currentSpawnPixelmon);
		}
		PhotonNetwork.room.SetCustomProperties(customProperties);
	}

	private void TryCatckPixelmon(string namePixelmon)
	{
		if (!hasPixelmon)
		{
			_photonView.RPC("PixelmonCaught", PhotonTargets.MasterClient, namePixelmon, PlayerBehavior.MyPlayer.photonView.ownerId);
		}
	}

	[PunRPC]
	public void PixelmonCaught(string namePixelmon, int id)
	{
		foreach (string key in _dictionaryPixelmons.Keys)
		{
			if (_dictionaryPixelmons[key] == id)
			{
				return;
			}
		}
		if (_dictionaryPixelmons[namePixelmon] != -1)
		{
			return;
		}
		_photonView.RPC("PlayerCatchPixelmon", PhotonTargets.All, namePixelmon, id);
		_dictionaryPixelmons[namePixelmon] = id;
		foreach (PixelmonBehaviour actualPixelmon in actualPixelmons)
		{
			if (actualPixelmon.namePixelmon == namePixelmon)
			{
				PhotonNetwork.Destroy(actualPixelmon.gameObject);
			}
		}
	}

	[PunRPC]
	private void PlayerCatchPixelmon(string namePixelmon, int id)
	{
		_dictionaryPixelmons[namePixelmon] = id;
		if (id == PlayerBehavior.MyPlayer.photonView.ownerId)
		{
			hasPixelmon = true;
		}
		UpdateInterface();
		PunTeams.Team team = PunTeams.Team.none;
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			if (listPlayer.photonView.ownerId == id)
			{
				listPlayer.SwitchFlag(true, namePixelmon);
				team = listPlayer.localTeam;
				listPlayer.UpdateIconPixelmon(true, listPlayer.localTeam);
			}
		}
		if (id == PlayerBehavior.MyPlayer.photonView.ownerId)
		{
			ShowPointerOnDocStantion(true);
			GameplayNotificationController.instance.AddNotification(ScriptLocalization.Get("Game_Common/Pixelmon_PlayerCatchPixelmon") + " " + ScriptLocalization.Get("Game_Common/" + namePixelmon + "_name"), 0);
			if (PixelmonController.MeCatchPixelmon != null)
			{
				PixelmonController.MeCatchPixelmon();
			}
		}
		else
		{
			GameplayNotificationController.instance.AddNotification(string.Format(ScriptLocalization.Get((team != TDMController.myTeam) ? "Game_Common/Pixelmon_EnemyCatchPixelmon" : "Game_Common/Pixelmon_OurCatchPixelmon"), ScriptLocalization.Get("Game_Common/" + namePixelmon + "_name")), 0);
		}
	}

	private void DropPixelmonInDocStantion(PunTeams.Team team)
	{
		if (!hasPixelmon)
		{
			return;
		}
		foreach (string key in _dictionaryPixelmons.Keys)
		{
			if (_dictionaryPixelmons[key] == PlayerBehavior.MyPlayer.photonView.ownerId)
			{
				_photonView.RPC("SendMasterCapturedPixelmon", PhotonTargets.MasterClient, team, PlayerBehavior.MyPlayer.photonView.ownerId, key);
				break;
			}
		}
	}

	[PunRPC]
	private void SendMasterCapturedPixelmon(PunTeams.Team team, int id, string namePixelmon)
	{
		if (_dictionaryPixelmons[namePixelmon] == id)
		{
			_photonView.RPC("CapturedPixelmon", PhotonTargets.All, team, id, namePixelmon);
		}
	}

	[PunRPC]
	private void CapturedPixelmon(PunTeams.Team team, int id, string namePixelmon)
	{
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			if (listPlayer.photonView.ownerId == id)
			{
				listPlayer.SwitchFlag(false, namePixelmon);
				listPlayer.UpdateIconPixelmon(false, team);
				listPlayer.Points++;
			}
		}
		if (PlayerBehavior.MyPlayer.photonView.ownerId == id)
		{
			CupsManager.Cups += 3;
			hasPixelmon = false;
		}
		switch (team)
		{
		case PunTeams.Team.blue:
			_blueTeamCapturedPixelmon.Add(namePixelmon);
			_blueDocStantion.SpawnCaughtPixelmon(FindPixelmon(namePixelmon));
			break;
		case PunTeams.Team.red:
			_redTeamCapturedPixelmon.Add(namePixelmon);
			_redDocStantion.SpawnCaughtPixelmon(FindPixelmon(namePixelmon));
			break;
		}
		_dictionaryPixelmons[namePixelmon] = -1;
		UpdateInterface();
		if (id == PlayerBehavior.MyPlayer.photonView.ownerId)
		{
			ShowPointerOnDocStantion(false);
			if (PixelmonController.MeDropPixelmon != null)
			{
				PixelmonController.MeDropPixelmon();
			}
		}
		int num = ((TDMController.myTeam != PunTeams.Team.blue) ? (_redTeamCapturedPixelmon.Count - _blueTeamCapturedPixelmon.Count) : (_blueTeamCapturedPixelmon.Count - _redTeamCapturedPixelmon.Count));
		if (_blueTeamCapturedPixelmon.Count != maxPixelmonsForWin && _redTeamCapturedPixelmon.Count != maxPixelmonsForWin)
		{
			switch (num)
			{
			case 1:
				GameplayNotificationController.instance.AddNotification(ScriptLocalization.Get("Game_Common/Pixelmon_OurAdvantage"), 0);
				break;
			case -1:
				GameplayNotificationController.instance.AddNotification(ScriptLocalization.Get("Game_Common/Pixelmon_EnemyAdvantage"), 0);
				break;
			}
		}
		if (PhotonNetwork.isMasterClient)
		{
			IncreaseScore(team, 1);
		}
	}

	public void UpdateInterface()
	{
		if (!(GameInfoWindow.instance != null))
		{
			return;
		}
		List<string> list = new List<string>();
		foreach (PixelmonBehaviour actualPixelmon in actualPixelmons)
		{
			list.Add("PixelmoonIcpn_" + actualPixelmon.namePixelmon);
		}
		foreach (string key in _dictionaryPixelmons.Keys)
		{
			if (_dictionaryPixelmons[key] == -1)
			{
				continue;
			}
			foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
			{
				if (listPlayer.photonView.ownerId != _dictionaryPixelmons[key])
				{
					continue;
				}
				string empty = string.Empty;
				foreach (PixelmonBehaviour actualPixelmon2 in actualPixelmons)
				{
					if (actualPixelmon2.namePixelmon == key)
					{
						list.Remove("PixelmoonIcpn_" + key);
					}
				}
				empty = "PixelmoonIcpn_" + ((listPlayer.localTeam != TDMController.myTeam) ? (key + " enemy") : (key + " our"));
				list.Add(empty);
			}
		}
		List<string> list2 = new List<string>();
		List<string> list3 = new List<string>();
		foreach (string item in _redTeamCapturedPixelmon)
		{
			list2.Add("PixelmoonIcpn_" + item);
		}
		foreach (string item2 in _blueTeamCapturedPixelmon)
		{
			list3.Add("PixelmoonIcpn_" + item2);
		}
		GameInfoWindow.instance.tdmInfoUI.pixelmonGrid.UpdateGrid(list2.ToArray(), list3.ToArray(), list.ToArray(), true);
	}

	public void DropPixelmonFromPlayer(PlayerBehavior player)
	{
		foreach (string key in _dictionaryPixelmons.Keys)
		{
			if (_dictionaryPixelmons[key] != player.photonView.ownerId)
			{
				continue;
			}
			Ray ray = default(Ray);
			RaycastHit hitInfo = default(RaycastHit);
			ray.origin = player.transform.position + Vector3.forward + Vector3.up * 3f;
			ray.direction = Vector3.down;
			Ray ray2 = default(Ray);
			RaycastHit hitInfo2 = default(RaycastHit);
			ray2.origin = player.transform.position;
			ray2.direction = Vector3.up;
			Vector3 vector = Vector3.zero;
			if ((Physics.Raycast(ray, out hitInfo, 1000f, 23) && (hitInfo.collider.tag == "Water" || hitInfo.collider.tag == "Car")) || (Physics.Raycast(ray2, out hitInfo2, 1000f, 23) && (hitInfo2.collider.tag == "Water" || hitInfo2.collider.tag == "Car")))
			{
				float num = 100000f;
				foreach (SpawnPointChief.SpawnPoint item in SpawnPointChief.instance.playerSpawnPointsNew)
				{
					float num2 = Vector3.Distance(player.transform.position, item.position);
					if (num > num2)
					{
						vector = item.position;
						num = num2;
					}
				}
			}
			if (vector == Vector3.zero)
			{
				vector = player.transform.position;
			}
			ray.origin = vector + Vector3.up;
			ray.direction = Vector3.down;
			if (Physics.Raycast(ray, out hitInfo, 1000f))
			{
				vector = hitInfo.point;
			}
			PhotonNetwork.InstantiateSceneObject("Pixelmon/" + key, vector, player.transform.rotation, 0, null);
			_dictionaryPixelmons[key] = -1;
			_photonView.RPC("SwitchInterface", PhotonTargets.All, player.photonView.ownerId, false, key);
			break;
		}
	}

	[PunRPC]
	private void SwitchInterface(int id, bool isShow, string namePixelmon)
	{
		if (!isShow)
		{
			_dictionaryPixelmons[namePixelmon] = -1;
		}
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			if (listPlayer.photonView.ownerId != id)
			{
				continue;
			}
			listPlayer.SwitchFlag(isShow, namePixelmon);
			listPlayer.UpdateIconPixelmon(isShow, listPlayer.localTeam);
			UpdateInterface();
			if (id == PlayerBehavior.MyPlayer.photonView.ownerId)
			{
				ShowPointerOnDocStantion(isShow);
				if (!isShow && PixelmonController.MeDropPixelmon != null)
				{
					PixelmonController.MeDropPixelmon();
				}
			}
		}
	}

	private IEnumerator WaitCountPlayers(int id, bool isShow, string namePixelmon)
	{
		while (GameController.instance.listPlayers.Count <= 1)
		{
			yield return null;
		}
		SwitchInterface(id, isShow, namePixelmon);
	}

	private void IncreaseScore(PunTeams.Team team, int val)
	{
		if (PhotonNetwork.isMasterClient)
		{
			TDMController.instance.AddTeamPoints(team, val);
			Check();
		}
	}

	private GameObject FindPixelmon(string namePixelmon)
	{
		foreach (Pixelmon listPixelmon in _listPixelmons)
		{
			if (listPixelmon.namePixelmon == namePixelmon)
			{
				return listPixelmon.go;
			}
		}
		return null;
	}

	[PunRPC]
	public void EndPixelmonRound(PunTeams.Team winner)
	{
		if (winner == TDMController.myTeam)
		{
			winnerTeam = TDMResult.Winner;
		}
		else if (winner == TDMController.enemyTeam)
		{
			winnerTeam = TDMResult.Loser;
		}
		else if (winner == PunTeams.Team.none)
		{
			winnerTeam = TDMResult.Draw;
		}
		PVPManager.instance.TimeOut();
		GameInfoWindow.instance.scrollsTdmScore.UpdateScores();
		if (PhotonNetwork.isMasterClient)
		{
			ExitGames.Client.Photon.Hashtable customProperties = PhotonNetwork.room.customProperties;
			if (customProperties.ContainsKey("StartRoundTime"))
			{
				customProperties["StartRoundTime"] = null;
			}
			else
			{
				customProperties.Add("StartRoundTime", null);
			}
			PhotonNetwork.room.SetCustomProperties(customProperties);
		}
	}

	public void SwitchMaster()
	{
		if (PhotonNetwork.room.customProperties.ContainsKey("IndesSpawnPixelmon"))
		{
			_currentSpawnPixelmon = (int)PhotonNetwork.room.customProperties["IndesSpawnPixelmon"];
		}
		else
		{
			_currentSpawnPixelmon = 0;
		}
		if (!PhotonNetwork.room.customProperties.ContainsKey("ListIndexPoint") || !PhotonNetwork.room.customProperties.ContainsKey("ListIndexPixelmon"))
		{
			GenerateListPixelmonsAndPoints();
			return;
		}
		_roomPropertiesPixelmon = PhotonNetwork.room.customProperties["ListIndexPixelmon"].ToString();
		_roomPropertiesPixelmonPoints = PhotonNetwork.room.customProperties["ListIndexPoint"].ToString();
	}

	[PunRPC]
	public void ConnectNewPlayer(PhotonPlayer photonPlayer)
	{
		if (!PhotonNetwork.isMasterClient)
		{
			return;
		}
		string text = string.Empty;
		foreach (string key in _dictionaryPixelmons.Keys)
		{
			string text2 = text;
			text = text2 + key + " " + _dictionaryPixelmons[key] + ",";
		}
		string text3 = string.Empty;
		foreach (string item in _redTeamCapturedPixelmon)
		{
			text3 = text3 + item + " ";
		}
		string text4 = string.Empty;
		foreach (string item2 in _blueTeamCapturedPixelmon)
		{
			text4 = text4 + item2 + " ";
		}
		_photonView.RPC("SendInfo", photonPlayer, text, text3, text4);
	}

	public void GetInfoFromMaster(PlayerBehavior player)
	{
		PhotonPlayer photonPlayer = PhotonPlayer.Find(player.photonView.ownerId);
		_photonView.RPC("ConnectNewPlayer", PhotonTargets.MasterClient, photonPlayer);
	}

	[PunRPC]
	private void SendInfo(string pixelmonInfo, string redTeamCaptured, string blueTeamCaptured)
	{
		StartCoroutine(UpdateInfoPixelmon(pixelmonInfo, redTeamCaptured, blueTeamCaptured));
	}

	private IEnumerator UpdateInfoPixelmon(string pixelmonInfo, string redTeamCaptured, string blueTeamCaptured)
	{
		while (TDMController.myTeam == PunTeams.Team.none)
		{
			yield return null;
		}
		_redDocStantion.ClearPixelmon();
		_blueDocStantion.ClearPixelmon();
		_redTeamCapturedPixelmon.Clear();
		_blueTeamCapturedPixelmon.Clear();
		ShowPointerOnDocStantion(false);
		UpdateColorObjects();
		string[] array = redTeamCaptured.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		foreach (string name in array)
		{
			CapturedPixelmon(PunTeams.Team.red, -1, name);
		}
		string[] array2 = blueTeamCaptured.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		foreach (string name2 in array2)
		{
			CapturedPixelmon(PunTeams.Team.blue, -1, name2);
		}
		string[] array3 = pixelmonInfo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
		foreach (string pixelmon in array3)
		{
			string[] info = pixelmon.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			if (_dictionaryPixelmons.ContainsKey(info[0]))
			{
				int currentPlayerId = 0;
				int.TryParse(info[1], out currentPlayerId);
				_dictionaryPixelmons[info[0]] = currentPlayerId;
				if (currentPlayerId != -1)
				{
					StartCoroutine(WaitCountPlayers(currentPlayerId, true, info[0]));
				}
			}
		}
	}

	private void ShowPointerOnDocStantion(bool showPointer)
	{
		if (showPointer)
		{
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				_pointer = PointerController.instance.AddPointer(_blueDocStantion.pointerOnDocStantio, 0f, "PixelmoonIcpn_Dokstation");
			}
			else if (TDMController.myTeam == PunTeams.Team.red)
			{
				_pointer = PointerController.instance.AddPointer(_redDocStantion.pointerOnDocStantio, 0f, "PixelmoonIcpn_Dokstation");
			}
		}
		else
		{
			PointerController.instance.RemovePointer(_pointer);
		}
	}
}
